<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Wall (User wall post)
 * @description : Show user's wall post & operations related to wall post
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Wall extends AUTH_Controller {

  public function index($user_name = NULL, $option_type = NULL, $wall_post_id = NULL) {

    // Show wall of current user if no or invalid username
    if (!$user_name) {
      $user_name = $this->current_user['user_name'];
    }

    // Setting the limit of wall posts to load
    $limit = $this->input->get('limit', TRUE);
    if (empty($limit) || !is_numeric($limit)) {
      $limit = 10;
    }

    // Cleaning the username
    $user_name = $this->sanitize($user_name);

    // If valid username
    if (!empty($user_name)) {

      // Gets the user details of the user with username
      $get_user_by_username = $this->user_model->get_user_by_username($user_name);

      // If there is a valid user
      if ($get_user_by_username['status'] == 1 && !empty($get_user_by_username['result'])) {

        // Setting user ids
        $post_user_id = $get_user_by_username['result']['user_id'];
        $post_from_user_id = $this->current_user['user_id'];

        // Update wall post read status to yes if it is the wall of current user
        if ($post_user_id == $post_from_user_id) {
          $this->wall_model->update_read_status($post_user_id);
        }

        // Delete wall post if the request is to delete & redirect back to wall
        if (!empty($option_type) && $option_type == 'delete' && !empty($wall_post_id) && is_numeric($wall_post_id)) {

          // Getting wall post owner
          $wall_post_by_user_id = NULL;
          $get_wall_post_by_id = $this->wall_model->get_wall_post_by_id($wall_post_id);
          if ($get_wall_post_by_id['status'] == 1 && !empty($get_wall_post_by_id['result'])) {
            $wall_post_by_user_id = $get_wall_post_by_id['result']['wall_post_from_user_id'];
          }

          // Checking weather deleting by wall owner or post owner or admin
          if ($wall_post_by_user_id == $this->current_user['user_id'] || $post_user_id == $this->current_user['user_id'] || $this->is_admin) {
            $this->like_model->del_likes_by_post('wall_post', $wall_post_id);
            $this->wall_model->del_wall_post_by_id($wall_post_id);
          }

          // Redirecting back to wall
          redirect('wall/' . $user_name);
          return;
        }

        // If the request type is post. a new wall post
        if ($this->is_post()) {

          // Setting from post status & message
          $form_status = 2;
          $form_msg = 'Error :(';

          // Cleaning the wall post (Removing if there any unwanted <br/> in the end)
          $wall_post_msg = $this->sanitize_html($this->input->post('wall_post', TRUE));

          // If valid wall post message
          if (!empty($wall_post_msg)) {

            // Calling the model function to insert new wall post
            $set_wall_post = $this->wall_model->set_wall_post($post_user_id, $post_from_user_id, $wall_post_msg);

            // Checking wheathe the insertion process is success
            if ($set_wall_post['status'] == 1 && !empty($set_wall_post['result'])) {

              $wall_post_id = $set_wall_post['result']['wall_post_id'];

              if ($post_user_id != $this->current_user['user_id']) {
                $toast_text = '@' . $this->current_user['user_name'] . ' posted on your wall <b>#W' . $wall_post_id . '</b>';
                @add_toast_notificaion($post_user_id, $toast_text, 'W' . $wall_post_id);
              }

              $form_status = 1;
              $form_msg = 'Success :)';

              // Setting the session flash data for notification
              $this->session->set_flashdata('wall_wall_post_id', $wall_post_id);
              $this->session->set_flashdata('wall_wall_post_hash', md5($wall_post_id . 'wall_post'));
            }
          }

          // Setting the session flash data to show popup messages
          $this->session->set_flashdata('wall_post_form_status', $form_status);
          $this->session->set_flashdata('wall_post_form_message', $form_msg);

          // Redirect back to the wall (Refresh page)
          redirect('wall/' . $user_name);
          return;
        }

        // If request is not post. Show wall posts
        // Calling model fucntion to get the wall posts of the selected user
        $get_my_wall_post = $this->wall_model->get_my_wall_post($post_user_id, $limit);

        // Calling model function to get some additional infromations releated to the user's wall 
        $get_my_wall_post_extras = $this->wall_model->get_my_wall_post_extras($post_user_id, $post_from_user_id);

        // Preparing the view data array
        $view_data = array();
        $view_data['menu_active'] = 'users';
        $view_data['limit'] = $limit;
        $view_data['user_data'] = $get_user_by_username['result'];
        $view_data['wall_post_data'] = $get_my_wall_post['result'];
        $view_data['wall_post_extras_data'] = $get_my_wall_post_extras['result'];

        // Loading the view and passing the view data
        $this->load_view('wall', $view_data);
        return;
      }
    }

    // If no valid username selected, Redirect to profile page
    redirect('profile');
    return;
  }

}

?>
